package com.canbot.u05.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.canbot.u05.R;

public class LineEdit extends EditText {

        private Context mContext;

        private Paint mPaint;

        /**
         * 右侧的删除图标
         */
        private Drawable del;

        /**
         * 是否显示图标
         */
        private boolean hasDeleteIcon;

        String lineText;


        private boolean isHasFocus;

        /**
         * @param context
         * @param hasDeleteIcon 是否显示图标，true显示，false不显示
         */
        public LineEdit(Context context, boolean hasDeleteIcon) {
                super(context);
                mContext = context;
                this.hasDeleteIcon = hasDeleteIcon;
                init();
        }

        public LineEdit(Context context, AttributeSet attrs) {
                super(context, attrs);
                mContext = context;
                init();
        }

        public LineEdit(Context context, AttributeSet attrs, int defStyleAttr) {
                super(context, attrs, defStyleAttr);
                mContext = context;
                init();
        }

        private void init() {
                mPaint = new Paint();
                mPaint.setStrokeWidth(2.0f);
                mPaint.setStyle(Paint.Style.STROKE);
                this.mPaint.setColor(getResources().getColor(R.color.line_edit_text_color));
                //del=mContext.getResources().getDrawable(R.drawable.edit_delete);
                Drawable[] drawables = getCompoundDrawables();
                del = drawables[2];
                //设置焦点变化的监听
                this.setOnFocusChangeListener(new FocusChangeListenerImpl());
                //设置EditText文字变化的监听
                this.addTextChangedListener(new TextWatcherImpl());

                //设置右边删除图标
                if (hasDeleteIcon) {
                        setDrawable();
                }
        }

        private class FocusChangeListenerImpl implements OnFocusChangeListener {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                        isHasFocus = hasFocus;
                        if (isHasFocus) {
                                //boolean isVisible=getText().toString().length()>=1;
                                setClearDrawableVisible(true);
                        }
                        else {
                                // setClearDrawableVisible(isVisible);
                        }
                }
        }

        //当输入结束后判断是否显示右边clean的图标
        private class TextWatcherImpl implements TextWatcher {

                @Override
                public void afterTextChanged(Editable s) {
                        //boolean isVisible=getText().toString().length()>=1;
                        setClearDrawableVisible(true);
                        lineText = getText().toString();
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

        }

        protected void setClearDrawableVisible(boolean isVisible) {
                Drawable rightDrawable;
                if (isVisible) {
                        rightDrawable = del;
                }
                else {
                        rightDrawable = null;
                }

                //setDrawable();
                setCompoundDrawablesWithIntrinsicBounds(null, null, rightDrawable, null);


        }


        private void setDrawable() {
                setCompoundDrawablesWithIntrinsicBounds(null, null, del, null);
        }

        @Override
        protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
                        this.getHeight() - 1, mPaint);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
                switch (event.getAction()) {

                        case MotionEvent.ACTION_UP:

                                boolean isClean = (event.getX() > (getWidth() - getTotalPaddingRight())) &&
                                        (event.getX() < (getWidth() - getPaddingRight()));
                                if (isClean) {
                                        setText("");
                                }
                                break;
                }

                return super.onTouchEvent(event);
        }

        @Override
        protected void finalize() throws Throwable {
                super.finalize();
        }

        public String getlineText() {

                return lineText;

        }
}
